package com.dcdata.xian;

import com.dcdata.xian.mapper.GjznCardMapper;
import com.dcdata.xian.mapper.GjznRouteMapper;
import com.dcdata.xian.mapper.GjznStationMapper;
import com.dcdata.xian.mapper.LineExtensionMapper;
import com.dcdata.xian.pojo.GjznCard;
import com.dcdata.xian.pojo.GjznRoute;
import com.dcdata.xian.pojo.GjznStation;
import com.dcdata.xian.pojo.LineExtension;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;


/**
 * @author suxm
 */

public class MysqlOperationDg {
    /**
     * SQL session.
     */
    private SqlSession sqlSession;
    /**
     * SQL session factory builder.
     */
    private final SqlSessionFactoryBuilder builder;
    /**
     * SQL session factory.
     */
    private SqlSessionFactory sqlSessionFactory;

    private LineExtensionMapper lineExtensionMapper;

    /**
     * Constructor, initialize SQL session builder.
     */
    public MysqlOperationDg() {
        builder = new SqlSessionFactoryBuilder();
    }


    /**
     * Connect to database, initialize related mapper.
     */
    public void connectToDatabase() {
        try {
            sqlSessionFactory = builder.build(Resources.getResourceAsStream("SqlMapConfig_dg.xml"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        sqlSession = sqlSessionFactory.openSession();

        lineExtensionMapper = sqlSession.getMapper(LineExtensionMapper.class);

    }

    void insertLineExtension(LineExtension lineExtension){
        lineExtensionMapper.insert(lineExtension);
        sqlSession.commit();
    }

    public SqlSession getSqlSession() {
        return sqlSession;
    }

    public LineExtensionMapper getLineExtensionMapper() {
        return lineExtensionMapper;
    }

    /**
     * Close database.
     */
    public void closeDatabase() {
        if (sqlSession != null) {
            sqlSession.close();
        }
    }
}
